﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Sec;
using ESOL.Common;
using ESOL.Common.Entity;
using DevExpress.XtraTreeList.Nodes;

namespace ESOL.WinUI.Sec
{
    public partial class frmManageMenu : ESOL.WinUI.BaseForm.frmBaseMdiClassicForm
    {
        #region Variable
        private SecMenuEntity secMenuFirstType;
        private SecMenuEntity secMenuSecType;
        #endregion

        #region Constructor
        public frmManageMenu()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void RefreshLeftTree()
        {
            treeLeft.Nodes.Clear();
            List<SecMenuEntity> listRoot = SecMenuBs.GetRootMenus();
            treeLeft.DataSource = listRoot;
            treeLeft.ExpandAll();
        }

        private void RefreshDataGrid(string menuId)
        {
            try
            {
                DataSet ds = SecMenuBs.GetDSMenusByParentID(menuId);
                if (ds != null && ds.Tables.Count > 0)
                {
                    gvMain.DataSource = ds.Tables[0];
                }
                else
                {
                    gvMain.DataSource = null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #endregion

        #region Event
        private void frmManageMenu_Load(object sender, EventArgs e)
        {
            RefreshLeftTree();
        }

        protected override void Click_View()
        {
            if (secMenuSecType == null)
            {
                MessageBox.Show("请选择一条数据！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainMenu frm = new frmMaintainMenu();
            frm.ThisActionType = ActionType.View;
            frm.MenuEntity = secMenuSecType;
            frm.ShowDialog();
        }

        protected override void  Click_Delete()
        {
            if (secMenuSecType == null)
            {
                MessageBox.Show("请选择一条数据！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            TreeListNode treeListNodeFocused = treeLeft.FocusedNode;
            DialogResult dr = MessageBox.Show("确定删除？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                bool successful = SecMenuBs.DeleteMenu(secMenuSecType);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    RefreshLeftTree();
                    if (treeListNodeFocused == null)
                    {
                        return;
                    }
                    foreach (TreeListNode treeListNode in treeLeft.Nodes)
                    {
                        if (treeListNode.GetValue("MenuId").Equals(treeListNodeFocused.GetValue("MenuId")))
                        {
                            treeLeft.SetFocusedNode(treeListNode);
                            break;
                        }
                    }
                    RefreshDataGrid(secMenuFirstType.MenuId);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void  Click_Modify()
        {
            if (secMenuSecType == null)
            {
                MessageBox.Show("请选择一条数据！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainMenu frm = new frmMaintainMenu();
            frm.ThisActionType = ActionType.Modify;
            frm.MenuEntity = secMenuSecType;
            TreeListNode treeListNodeFocused = treeLeft.FocusedNode;
            if (frm.ShowDialog() == DialogResult.OK)
            {                
                RefreshLeftTree();
                if (treeListNodeFocused == null)
                {
                    return;
                }
                foreach (TreeListNode treeListNode in treeLeft.Nodes)
                {
                    if (treeListNode.GetValue("MenuId").Equals(treeListNodeFocused.GetValue("MenuId")))
                    {
                        treeLeft.SetFocusedNode(treeListNode);
                        break;
                    }
                }
                RefreshDataGrid(secMenuFirstType.MenuId);
            }
        }

        protected override void  Click_New()
        {
            frmMaintainMenu frm = new frmMaintainMenu();
            frm.ThisActionType = ActionType.New;
            TreeListNode treeListNodeFocused = treeLeft.FocusedNode;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                RefreshLeftTree();
                if (treeListNodeFocused == null)
                {
                    return;
                }
                foreach (TreeListNode treeListNode in treeLeft.Nodes)
                {
                    if (treeListNode.GetValue("MenuId").Equals(treeListNodeFocused.GetValue("MenuId")))
                    {
                        treeLeft.SetFocusedNode(treeListNode);
                        break;
                    }
                }
                RefreshDataGrid(secMenuFirstType.MenuId);
            }
        }

        private void btnMulitModify_Click(object sender, EventArgs e)
        {
            if (gvMain.DataSource == null)
            {
                MessageBox.Show("没有找到要修改的项！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DataTable dt = (DataTable)gvMain.DataSource;
            frmMaintainMenuMulit frm = new frmMaintainMenuMulit(dt);
            frm.ThisActionType = ActionType.Modify;
            TreeListNode treeListNodeFocused = treeLeft.FocusedNode;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                RefreshLeftTree();
                if (treeListNodeFocused == null)
                {
                    return;
                }
                foreach (TreeListNode treeListNode in treeLeft.Nodes)
                {
                    if (treeListNode.GetValue("MenuId").Equals(treeListNodeFocused.GetValue("MenuId")))
                    {
                        treeLeft.SetFocusedNode(treeListNode);
                        break;
                    }
                }
                RefreshDataGrid(secMenuFirstType.MenuId);
            }
        }

        private void btnRole_Click(object sender, EventArgs e)
        {
            if (secMenuSecType == null)
            {
                MessageBox.Show("Please select an object first!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainMenuRole frm = new frmMaintainMenuRole();
            frm.MenuEntity = secMenuSecType;
            frm.ShowDialog();
        }

        private void btnImportObject_Click(object sender, EventArgs e)
        {
            frmMaintainImportObjects frm = new frmMaintainImportObjects();
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }
        #endregion

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            secMenuSecType = new SecMenuEntity(gridView1.GetDataRow(e.RowHandle));
        }

        private void treeLeft_AfterFocusNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            secMenuFirstType = SecMenuBs.GetMenuByID(e.Node.GetValue("MenuId").ToString());
            RefreshDataGrid(secMenuFirstType.MenuId);
        }


    }
}
